import axios from 'axios';
import { Toast } from 'vant';

// const bastUrl = process.env.NODE_ENV === 'production' ? '/kpi' : '/api';
// const bastUrl = '/api';
// const bastUrl = 'http://192.168.2.20:9900/api';
const bastUrl = 'http://192.168.0.137:9900/api';
axios.defaults.timeout = 5000;

function requestBodyRemark(body) {
  const bodyInfo = body;
  if (!bodyInfo.menuId) {
    bodyInfo.menuId = '666';
  }
  bodyInfo.termIP = '23';
  bodyInfo.termMac = '23';
  bodyInfo.smrCd = '23';
  bodyInfo.tranDt = '23';
  bodyInfo.tranTm = '23';
  bodyInfo.mqNoSend = 'true';
  bodyInfo.version = '1';
  bodyInfo.chnlCode = '10202';
  bodyInfo.apiCode = '12';
  bodyInfo.globleSeqNo = '12121212121212121212121212121212';
  bodyInfo.tlrNo = '12';
  bodyInfo.brchCd = '23';
  bodyInfo.tlrTp = '32';
  bodyInfo.tlrLvl = '23';
  bodyInfo.accessToken = '23233223233223';
  bodyInfo.fromHttp = 'true';
  bodyInfo.fromChannel = 'true';
  bodyInfo.terminalNo = '1';
  bodyInfo.language = 'zh-cn';
  return bodyInfo;
}

function requestFormat(url, body) {
  console.log(body);
  return {
    ...body,
    termIP: '23',
    termMac: '23',
    smrCd: '23',
    tranDt: '23',
    tranTm: '23',
    menuId: body.menuId ? body.menuId : '666',
    mqNoSend: 'true',
    version: '1',
    chnlCode: '10202',
    apiCode: '12',
    serviceName: url,
    globleSeqNo: '12121212121212121212121212121212',
    tlrNo: '12',
    brchCd: '23',
    tlrTp: '32',
    tlrLvl: '23',
    accessToken: '23233223233223',
    fromHttp: 'true',
    fromChannel: 'true',
    terminalNo: '1',
    language: 'zh-cn'
  };
}

axios.interceptors.request.use((config) => {
  const Config = config;
  // 解析token
  Config.headers['Content-Type'] = 'application/json';
  return Config;
}, (err) => Promise.reject(err));

axios.interceptors.response.use((response) => {
  if (response.status === 200) {
    return Promise.resolve(response);
  }
  return Promise.reject(response);
}, (error) => {
  if (error.response.status) {
    switch (error.response.status) {
      case 404:
        // code: 提示 404 错误
        Toast.fail({ position: 'bottom', message: '接口404' });
        break;
      case 500:
        // code: 提示 500  或其他错误
        break;
      default:
        break;
    }
  }
  return Promise.reject(error.response);
});

export function get(url, params) {
  return new Promise((resolve, reject) => {
    // let dataStr = '';
    // Object.keys(params).forEach((key) => {
    //   dataStr += `/${params[key]}`;
    // });
    axios.get(url, { params }).then((res) => {
      resolve(res.data);
    }).catch((error) => {
      reject(error.data);
    });
  });
}

export function post(url, params) {
  Toast.loading({
    duration: 0,
    message: '加载中...',
    forbidClick: true,
    loadingType: 'spinner'
  });
  const remarkParam = requestFormat(url, params);
  return new Promise((resolve, reject) => {
    // axios.post(url, QS.stringify(params)).then(res => {
    axios.post(`${bastUrl}/${url}`, JSON.stringify(remarkParam)).then((res) => {
      Toast.clear();
      if (res.data.retCode === 'CIP0000000') {
        resolve(res.data);
      } else {
        Toast(res.data.message);
      }
    }).catch((error) => {
      Toast.clear();
      Toast({
        position: 'bottom',
        message: '无法连接服务器，\n请检查网络是否可用',
        // duration: 0,
        className: 'toast_error'
      });
      reject(error.data);
    });
  });
}

export function postView(vm, url, params) {
  Toast.loading({
    duration: 0,
    message: '加载中...',
    forbidClick: true,
    loadingType: 'spinner'
  });
  const remarkParm = requestBodyRemark(params);
  console.log('*********remarkParm start*********');
  console.log(remarkParm);
  console.log('*********remarkParm end*********');
  return new Promise((resolve, reject) => {
    // axios.post(url, QS.stringify(params)).then(res => {
    axios.post(`${bastUrl}/${url}`, JSON.stringify(remarkParm)).then((res) => {
      Toast.clear();
      if (res.data.retCode === 'CIP0000000') {
        resolve(res.data);
      } else {
        Toast(res.data.message);
      }
    }).catch((error) => {
      Toast.clear();
      reject(error.data);
    });
  });
}
